package com.alangyun.remind.service.excel;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.alangyun.remind.error.MindException;
import com.alangyun.remind.repository.data.OrganCaution;
import com.alangyun.remind.repository.data.OrganDataset;

public class OrganCautionReader extends AbstractExcelReader {
	private static final int ROW_OFFSET = 1;

	@Override
	protected ParseResult readSheet(Sheet sheet) {

		ParseResult ret = new ParseResult();

		int rowIndex = sheet.getFirstRowNum();
		int rowCount = sheet.getLastRowNum();

		OrganDataset list = OrganDataset.create();
		ret.setObject(list);

		for (int index = rowIndex + ROW_OFFSET; index <= rowCount; index++) {
			Row row = sheet.getRow(index);
			if(row==null)
				break;
			
			int colIndex = row.getFirstCellNum();
			try {
				String name = row.getCell(colIndex++).getStringCellValue();
				int plan = PoiUtils.getIntValue(row.getCell(colIndex++));
				int fact = PoiUtils.getIntValue(row.getCell(colIndex++));
				double score = PoiUtils.getDoubleValue(row.getCell(colIndex));
				if(name==null||name.trim().length()==0)
					throw new MindException("机构名称不能为空");
				if(plan<=0||fact<0)
					throw new MindException("数据值不合法");

				list.getDatas().add(OrganCaution.create(name.trim(), plan, fact, score));
			}catch(MindException me) { 
				ret.addError("第" + index + "行:" + me.getMessage());
				e("解析组织机构预警数据失败", me);
			}catch (Exception ex) {
				ret.addError("第" + index + "行数据有错");
				e("解析组织机构预警数据失败", ex);
			}
		}

		return ret;
	}

}
